//
//  ProvinceTableViewController.m
//  helloword
//
//  Created by chen on 17/3/8.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "ProvinceTableViewController.h"
#import "CityTableViewController.h"
#define CellIdentifier @"CellIdentifier"

@interface ProvinceTableViewController ()

@property (strong,nonatomic) NSDictionary *dictData;
@property (strong,nonatomic) NSArray *listData;

@end

@implementation ProvinceTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"province_city" ofType:@"plist"];
    _dictData = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
    _listData = [_dictData allKeys];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _listData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSInteger row = [indexPath row];
    cell.textLabel.text = _listData[row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:@"ShowSelectedProvince" sender:self];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowSelectedProvince"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSInteger selectedIndex = [indexPath row];
        
        CityTableViewController *cityTableViewController = segue.destinationViewController;
        NSString *selectedName = _listData[selectedIndex];
        cityTableViewController.listData = _dictData[selectedName];
        cityTableViewController.title = selectedName;
    }
}

@end
